/**
 * ============================================================
 * ======= 多音字 ===
 */
import { reactive } from "vue";
import { pinyin, convert } from 'pinyin-pro';

export function usePingYi(){
	const statePingYi = reactive({
		basis: '',
		pingyi: [],
		groupWords: []
	})
	// 数据源
	const polyphonicGetLibraryWords = (words) => {
		if(!words) {
			return null
		}
		const wordsObj = JSON.parse(words)
		const wordsArr = [];
		const pingyi = [];
		for(let key in wordsObj) {
			const splitKey = key.split("|")
			const split = wordsObj[key].split("|")
			wordsArr.push(split[0])
			// 拼音
			const py = {}
			py.rowId = splitKey[0]
			py.id = splitKey[1]
			py.words = split[0]
			py.pingyi = split[1].split("-")
			pingyi.push(py)
		}
		// result
		return {
			words: wordsArr,
			pingyi
		}
	}
	// 渲染
	const polyphonicRenderWords = (words) => {
		if(!words || words && !words.length) {
			return []
		}
		const arr = []
		words.forEach(item => {
			const formatWords = polyphonicGetLibraryWords(item)?.pingyi;
			const obj = {}
			formatWords.forEach(w => {
				obj[`${w.rowId}|${w.wordsId}`] = `${w.words}|${w.words}`
			})
			arr.push(JSON.stringify(obj))
		})
		return arr
	}
	// 阅读解析拼音
	const polyphonicPingYiWords = (words) => {
		//  {text: "端端正正|歪歪扭扭", rowId: "694", id: "1"}
		if(!words || words && !words.length) {
			return []
		}
		const result = []
		words.forEach(item => {
			const formatWords = polyphonicGetLibraryWords(item)?.pingyi;
			
			formatWords.forEach(w => {
				const text = w.pingyi.map(t => w.words)
				const obj = {
					...w,
					text: text.join("|")
				}
				result.push(obj)
			})
		})
		return result
	}
	// 获取拼音
	const getWordsPingYi = (words, every) => {
		words = words && every ? words.split() : words || null
		if(!words) {
			return
		}
		const result = []
		
		if(every) {
			words.forEach(item => {
				const obj = {
					pingyi: pinyin(item, { type: 'array', multiple: true }),
					words: item.replace(/\s*/g, "")
				}
				result.push(obj)
			})
			return result
		}else{
			
			return {
				pingyi: pinyin(words, { type: 'array', multiple: true }),
				words
			}
		}
		
	}
	// 获取组词
	const getGroupWords = (words) => {
		let value = words
		if(words.indexOf('|') !== -1) {
			value = words.split('|')[0]
		}
		const wordsArray = statePingYi.basis.split(' ')
		const result = wordsArray.filter(word => word.includes(value))
		
		return result
	}
	// 根据拼音获取组词
	const getGroupWordsByPingYi = (words) => {
		let result = getGroupWords(words)
		result = getWordsPingYi(result)
		const resultArray = [];
		statePingYi.pingyi.forEach(item => {
			const matchData = result.filter(obj => obj.pingyi.includes(item))
			resultArray.push(matchData)
		})
		statePingYi.groupWords = resultArray
		return resultArray
	}
	// 声调转数字
	const useGetToneToNumber = (tone) => {
		// 符号转数字(symbolToNum)
		const tones = convert(tone, { format: 'symbolToNum' }); // 'pin1 yin1'
		return tones
	}
	return {
		statePingYi,
		polyphonicGetLibraryWords,
		polyphonicRenderWords,
		polyphonicPingYiWords,
		getGroupWords,
		getGroupWordsByPingYi,
		getWordsPingYi,
		useGetToneToNumber,
	}
}